package weaver.interfaces.its.action;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.email.EmailWorkRunnable;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/11/14
 * @Description 责任人1节点后发送邮件提醒
 * @Author AdminZm
 */
public class PersonResponsibleOneSendEmailAction implements Action {

    BaseBean baseBean = new BaseBean();

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestId = requestInfo.getRequestid();
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        baseBean.writeLog("PersonResponsibleOneSendEmailAction start:" + requestId + "、" + billTableName);
        try {
            RecordSet rs = new RecordSet();
            String url = rs.getPropValue("itest_semi_zmnj_prop", "workflow_url") + requestId;
            String sql = "select a.bt,a.sqr,a.zrrjl, b.email as sqrEmail, c.email as zrrjlEmail from " + billTableName + " a " +
                    " LEFT JOIN hrmresource b on a.sqr = b.id " +
                    " LEFT JOIN hrmresource c on a.zrrjl = c.id " +
                    " where a.requestId = ? ";
            rs.executeQuery(sql, requestId);
            if (rs.next()) {
                String sqrEmail = rs.getString("sqrEmail");
                String bt = rs.getString("bt");
                String zrrjlEmail = rs.getString("zrrjlEmail");

                String content = "<p>&nbsp; &nbsp; 根因调查已结束，请到流程 <a href=" + url + ">" + bt + "</a> 中完成相关文件上传！</p>";
                baseBean.writeLog("content is:" + content);
                if (StringUtils.isNotEmpty(zrrjlEmail)) {
                    Map<String, String> paramMap = new HashMap<>();
                    paramMap.put("email", zrrjlEmail);
                    paramMap.put("content", content);
                    sendEmail(paramMap);
                }
                content = "<p>&nbsp; &nbsp; 根因调查已结束，请到流程 <a href=" + url + ">" + bt + "</a> 中查看！</p>";
                if (StringUtils.isNotEmpty(sqrEmail)) {
                    Map<String, String> paramMap = new HashMap<>();
                    paramMap.put("email", sqrEmail);
                    paramMap.put("content", content);
                    sendEmail(paramMap);
                }
            }
        } catch (Exception e) {
            baseBean.writeLog("PersonResponsibleOneSendEmailAction error:" + e);
        }
        return Action.SUCCESS;
    }

    public Map<String, Object> sendEmail(Map<String, String> paramMap) {
        HashMap<String, Object> resultMap = new HashMap<>();
        try {
            String email = paramMap.get("email");
            //发送邮件内容
            String sendEmailContent = paramMap.get("content");
            EmailWorkRunnable ewr = new EmailWorkRunnable(email, "文件上传提醒", sendEmailContent);
            boolean result = ewr.emailCommonRemind();
            resultMap.put("sendState", result);
            baseBean.writeLog(this.getClass().getName() + "-email-" + result);
        } catch (Exception e) {
            resultMap.put("errMsg", e.getMessage());
        }
        return resultMap;
    }
}
